//
//  HXDEmoticonButton.swift
//  weibo
//
//  Created by huxuedong on 15/12/17.
//  Copyright © 2015年 huxuedong. All rights reserved.
//

import UIKit

class HXDEmoticonButton: UIButton {

    var emoticon: HXDEmoticon? {
        didSet {
            guard let emoticon = emoticon else {
                return
            }
            //如果不是Emoji图片
            if !emoticon.isEmoji {
                //根据全路径设置图片
                let image = UIImage(named: emoticon.fullPath!, inBundle: HXDEmoticonTools.sharedInstance.emoticonBundel, compatibleWithTraitCollection: nil)
                setImage(image, forState: .Normal)
                setTitle(nil, forState: .Normal)
            } else {
                //将图片名字转换成Emoji图片
                setTitle((emoticon.code! as NSString).emoji(), forState: .Normal)
                setImage(nil, forState: .Normal)
            }
        }
    }
    
    func showPopView(popView: HXDEmoticonPopView) {
        //按钮在屏幕上的位置
        let rect = self.convertRect(bounds, toView: nil)
        popView.center.x = CGRectGetMidX(rect)
        popView.y = CGRectGetMaxY(rect) - popView.height
        popView.emoticonButton.emoticon = emoticon
        let window = UIApplication.sharedApplication().windows.last!
        window.addSubview(popView)
    }

}
